/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.shinyray.org.view;

import com.icesoft.faces.component.selectinputtext.SelectInputText;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

/**
 * 组织机构自动
 * @author Administrator
 */
public class OrgAutoCompleteBean {
    private Logger logger=Logger.getLogger(OrgAutoCompleteBean.class.getName());
    private OrgnizationVO currentOrg;
    // list of possible matches.
    private List matchesList = new ArrayList();
    // list of cities, used for auto complete list.
    private static List dictionary;
    
    public void updateList(ValueChangeEvent event) {
        // get a new list of matches.
        setMatches(event);
        // Get the auto complete component from the event and assing
        if (event.getComponent() instanceof SelectInputText) {
            SelectInputText autoComplete =
                    (SelectInputText) event.getComponent();
            // if no selected item then return the previously selected item.
            if (autoComplete.getSelectedItem() != null) {
                currentOrg = (OrgnizationVO) autoComplete.getSelectedItem().getValue();
            }
            // otherwise if there is a selected item get the value from the match list
            else {
                OrgnizationVO tempOrg = getMatch(autoComplete.getValue().toString());
                if (tempOrg != null) {
                    currentOrg = tempOrg;
                }
            }
        }
    }

    public OrgnizationVO getCurrentOrg() {
        return currentOrg;
    }

    public void setCurrentOrg(OrgnizationVO currentOrg) {
        this.currentOrg = currentOrg;
    }

    public  List getDictionary() {
        return dictionary;
    }

    public  void setDictionary(List dictionary) {
        OrgAutoCompleteBean.dictionary = dictionary;
    }

    public List getMatchesList() {
        return matchesList;
    }



    private OrgnizationVO getMatch(String value) {
        OrgnizationVO result = null;
        if (matchesList != null) {
            SelectItem si;
            Iterator iter = matchesList.iterator();
            while (iter.hasNext()) {
                si = (SelectItem) iter.next();
                if (value.equals(si.getLabel())) {
                    result = (OrgnizationVO) si.getValue();
                }
            }
        }
        return result;
    }
    /**
     * Utility method for building the match list given the current value of the
     * SelectInputText component.
     *
     * @param event
     */
    private void setMatches(ValueChangeEvent event) {

        Object searchWord = event.getNewValue();
        
        int maxMatches = ((SelectInputText) event.getComponent()).getRows();
        List matchList = new ArrayList(maxMatches);

        try {

            int insert = Collections.binarySearch(dictionary, searchWord,
                                                  AutoCompleteOrgDictionary.LABEL_COMPARATOR);

            // less then zero if wer have a partial match
            if (insert < 0) {
                insert = Math.abs(insert) - 1;
            }

            for (int i = 0; i < maxMatches; i++) {
                // quit the match list creation if the index is larger then
                // max entries in the dictionary if we have added maxMatches.
                if ((insert + i) >= dictionary.size() ||
                    i >= maxMatches) {
                    break;
                }
                matchList.add(dictionary.get(insert + i));
            }
        } catch (Throwable e) {
            logger.log(Level.SEVERE,"Erorr finding autocomplete matches", e);
        }
        // assign new matchList
        if (this.matchesList != null) {
            this.matchesList.clear();
            this.matchesList = null;
        }
        this.matchesList = matchList;
    }
}
